﻿'
' Will Strohl (wills@strohlsitedesign.com)
' http://www.willstrohl.com
'
'Copyright (c) 2009, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of StrohlSiteDesign.com nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports System.Collections
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Web.UI.WebControls

Namespace WillStrohl.Modules.FeedHandler

    ''' <summary>
    ''' GenericSwap - Swap and Sorting support for generic collections of custom objects.
    ''' </summary>
    ''' <remarks></remarks>
    ''' <history>
    ''' </history>
    Public Class GenericSwap

        ''' <summary>
        ''' Swaps two custom objects. Example: 
        ''' Swap(Of CarRateResultsInfo)(CarRateResultsInfo_A, CarRateResultsInfo_B))
        ''' </summary>
        ''' <typeparam name="T">The object type to sort</typeparam>
        ''' <param name="a">The first object (Of T)</param>
        ''' <param name="b">The second object (Of T)</param>
        ''' <remarks></remarks>
        Public Shared Sub Swap(Of T)(ByRef a As T, ByRef b As T)
            Dim temp As T
            temp = a
            a = b
            b = temp
        End Sub

    End Class

    ''' <summary>
    ''' Sorts a generic list of a custom object. Example: 
    ''' myList.Sort(New GenericComparer(Of SupplierVendorInfo)("SupplierVendorName", SortDirection.Ascending))
    ''' </summary>
    ''' <typeparam name="T">The object type to sort</typeparam>
    ''' <remarks>
    ''' Example: myList.Sort(New GenericComparer(Of SupplierVendorInfo)("SupplierVendorName", SortDirection.Ascending))
    ''' </remarks>
    ''' <history>
    ''' </history>
    Public Class GenericComparer(Of T)
        Implements IComparer(Of T)

        Private m_sortDirection As SortDirection

        Public Property SortDirection() As SortDirection
            Get
                Return Me.m_sortDirection
            End Get
            Set(ByVal value As SortDirection)
                Me.m_sortDirection = value
            End Set
        End Property

        Private sortExpression As String

        Public Sub New(ByVal sortExpression As String, ByVal sortDirection As SortDirection)
            Me.sortExpression = sortExpression
            Me.m_sortDirection = sortDirection
        End Sub

        Public Function Compare(ByVal x As T, ByVal y As T) As Integer Implements System.Collections.Generic.IComparer(Of T).Compare

            Dim propertyInfo As PropertyInfo = GetType(T).GetProperty(sortExpression)
            Dim obj1 As IComparable = DirectCast(propertyInfo.GetValue(x, Nothing), IComparable)
            Dim obj2 As IComparable = DirectCast(propertyInfo.GetValue(y, Nothing), IComparable)

            If SortDirection = SortDirection.Ascending Then
                Return obj1.CompareTo(obj2)
            Else
                Return obj2.CompareTo(obj1)
            End If

        End Function

    End Class

End Namespace